import requests
from bs4 import  BeautifulSoup
def gethtml(url):
    try:
        re=requests.get(url,timeout=10)
        re.raise_for_status()
        re.encoding="utf-8"
        print("获取成功")
        return re.text
    except:
        print("获取失败")
        return ""

def gethtmldata(html):
    soup=BeautifulSoup(html,"html.parser")
    trs=soup.find_all("tr")
    all_ls=[]
    for tr in trs:
        ls=[]
        for td in tr:
            if td.string!="\n":
                ls.append(td.string)
        all_ls.append(ls)
    return all_ls

def writedatas(ls):
    with open(r"中国大学排名.csv","w")as f:
        for line in ls:
            f.write(",".join(line[0:4])+"\n")

def main():
    url="http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html"
    html=gethtml(url)
    writedatas(gethtmldata(html))

if __name__ == '__main__':
    main()